package leetcode;

import org.omg.CORBA.INTERNAL;

import java.util.ArrayList;
import java.util.TreeSet;

/**
 * @author noob
 * @version 1.0
 * @date 2021/3/18 13:15
 */
public class Solution349 {
    public int[] intersection(int[] nums1, int[] nums2) {

        TreeSet<Integer> set = new TreeSet<>();
        for (int i = 0; i < nums1.length; i++) {
            set.add(nums1[i]);
        }

        ArrayList<Integer> list = new ArrayList<>();
        for(int num:nums2){
            //包含再添加，删除---就取交集了
            if(set.contains(num)){
                list.add(num);
                set.remove(num);
            }
        }



        int[] res = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            res[i] = list.get(i);
        }
        return res;

    }


    public static void main(String[] args) {
        int[] nums1 = {1,2,2,1};
        int[] nums2 = {2,2};
        int[] ints = new Solution349().intersection(nums1, nums2);
        for(int n:ints){
            System.out.print(n);
        }
    }
}
